package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.PurchaseList;
import com.atguigu.jxc.entity.PurchaseListGoods;
import com.atguigu.jxc.entity.User;
import com.atguigu.jxc.service.PurchaseListService;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @Author zhmwi
 * @Date 2021/9/5
 * @Dersciption 进货管理
 */
@RestController
@RequestMapping("purchaseListGoods")
public class PurchaseListController {

    @Autowired
    private PurchaseListService purchaseListService;

    /**
     * 保存进货单
     * @param purchaseList
     * @param purchaseListGoodsStr
     * @param session
     * @return
     */
    @PostMapping("save")
    public ServiceVO save(PurchaseList purchaseList, String purchaseListGoodsStr, HttpSession session){
        User user = (User) session.getAttribute("currentUser");
        Integer userId = user.getUserId();
        //进货单
        purchaseList.setUserId(userId);
        //进货商品单
        Gson gson = new Gson();
        List<PurchaseListGoods> purchaseListGoodsList = gson.fromJson(purchaseListGoodsStr, new TypeToken<List<PurchaseListGoods>>(){}.getType());
        //保存进货单 保存进货商品单
        Boolean isSave = purchaseListService.save(purchaseList,purchaseListGoodsList);
        if (isSave){
            return new ServiceVO(100,"请求成功");
        }else{
            return new ServiceVO(300,"请求失败");
        }
    }

}
